package tw.chen.unlock;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class TestActivity extends Activity 
{
	private TextView value, axis;
	private TextView text_n, text_nw, text_ne
	, text_s, text_sw, text_se, text_w, text_e;
	
	private int pos_limit = 30, neg_limit = -30;
	private int AXIS, DRIFT;
	
	String[] array;
	
	float[] acc_value = new float[3];
	float[] mag_value = new float[3];
	
	SensorManager sManager;
	Sensor ACC, MAG;
	
	SharedPreferences setting;
	Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		
		setting = getSharedPreferences(Values.SETTING, Values.SETTING_PAGE);
		
		sManager = (SensorManager)getSystemService(SENSOR_SERVICE);
		ACC = sManager.getDefaultSensor(Values.ACCELEROMETER);
		MAG = sManager.getDefaultSensor(Values.MAGNETIC);
		

		value = (TextView)findViewById(R.id.test_value);
		axis = (TextView)findViewById(R.id.test_axis);
		text_n = (TextView)findViewById(R.id.text_n);
		text_nw = (TextView)findViewById(R.id.text_nw);
		text_ne = (TextView)findViewById(R.id.text_ne);
		text_s = (TextView)findViewById(R.id.text_s);
		text_sw = (TextView)findViewById(R.id.text_sw);
		text_se = (TextView)findViewById(R.id.text_se);
		text_w = (TextView)findViewById(R.id.text_w);
		text_e = (TextView)findViewById(R.id.text_e);
		
		array = getResources().getStringArray(R.array.direction);
		
		init();
	}
	
	private void init()
	{
		AXIS = setting.getInt(Values.AXIS_TYPE, Values.AXIS_Y);
		switch (AXIS)
		{
		case Values.AXIS_X:
			sManager.registerListener(mListener, ACC, Values.rateUs_NORMAL);
			sManager.registerListener(mListener, MAG, Values.rateUs_NORMAL);
			axis.setText("X");
			break;
		case Values.AXIS_Y:
			sManager.registerListener(mListener, ACC, Values.rateUs_NORMAL);
			sManager.registerListener(mListener, MAG, Values.rateUs_NORMAL);
			axis.setText("Y");
			break;
		case Values.AXIS_Z:
			sManager.registerListener(mListener, ACC, Values.rateUs_FAST);
			sManager.registerListener(mListener, MAG, Values.rateUs_FAST);
			axis.setText("Z");
			text_n.setVisibility(View.VISIBLE);
			text_nw.setVisibility(View.VISIBLE);
			text_ne.setVisibility(View.VISIBLE);
			text_s.setVisibility(View.VISIBLE);
			text_sw.setVisibility(View.VISIBLE);
			text_se.setVisibility(View.VISIBLE);
			text_w.setVisibility(View.VISIBLE);
			text_e.setVisibility(View.VISIBLE);
			break;
		}
		
		int angle = setting.getInt(Values.DEGREES, 0);
		if (angle != 0)
		{
			pos_limit = angle;
			neg_limit = 0 - angle;
		}
		
		DRIFT = setting.getInt(Values.DRIFT, 10);
	}
	
	private SensorEventListener mListener = new SensorEventListener()
	{
		@Override
		public void onAccuracyChanged(Sensor arg0, int arg1) {}

		@Override
		public void onSensorChanged(SensorEvent event) 
		{
			switch (event.sensor.getType())
			{
			case Sensor.TYPE_ACCELEROMETER:
				acc_value = event.values;
				break;
			case Sensor.TYPE_MAGNETIC_FIELD:
				mag_value = event.values;
				break;
			}
			calculateDegrees();
		}
	};
	
	private void calculateDegrees()
	{
		float[] mValue = new float[3];
		float[] mR = new float[9];
		
		SensorManager.getRotationMatrix(mR, null, acc_value, mag_value);
		SensorManager.getOrientation(mR, mValue);

		for(int i = 0; i < mValue.length; i++)
		{
			mValue[i] = (float)Math.toDegrees(mValue[i]);
		}
		
		int x = (int)mValue[1];
		int y = (int)mValue[2];
		int z = (int)mValue[0];
		if (z < 0)
			z = 180 + (180 - Math.abs(z));
		
		switch (AXIS)
		{
		case Values.AXIS_X:
			value.setText(String.valueOf(x));
			if ((x > pos_limit) || (x < neg_limit))
				value.setTextColor(Color.RED);
			else
				value.setTextColor(Color.BLACK);
			break;
		case Values.AXIS_Y:
			value.setText(String.valueOf(y));
			if ((y > pos_limit) || (y < neg_limit))
				value.setTextColor(Color.RED);
			else
				value.setTextColor(Color.BLACK);
			break;
		case Values.AXIS_Z:
			value.setText(Integer.toString(z));
			resetColor();
			if((z > 360 - DRIFT) || (z < DRIFT))
				text_n.setTextColor(Color.RED);
			else if ((z > 45 - DRIFT) && (z < 45 + DRIFT))
				text_ne.setTextColor(Color.RED);
			else if ((z > 90 - DRIFT) && (z < 90 + DRIFT))
				text_e.setTextColor(Color.RED);
			else if ((z > 135 - DRIFT) && (z < 135 + DRIFT))
				text_se.setTextColor(Color.RED);
			else if ((z > 180 - DRIFT) && (z < 180 + DRIFT))
				text_s.setTextColor(Color.RED);
			else if ((z > 225 - DRIFT) && (z < 225 + DRIFT))
				text_sw.setTextColor(Color.RED);
			else if ((z > 270 - DRIFT) && (z < 270 + DRIFT))
				text_w.setTextColor(Color.RED);
			else if ((z > 315 - DRIFT) && (z < 315 + DRIFT))
				text_nw.setTextColor(Color.RED);
			break;
		}
	}
	
	private void resetColor()
	{
		text_n.setTextColor(Color.BLACK);
		text_nw.setTextColor(Color.BLACK);
		text_ne.setTextColor(Color.BLACK);
		text_s.setTextColor(Color.BLACK);
		text_sw.setTextColor(Color.BLACK);
		text_se.setTextColor(Color.BLACK);
		text_w.setTextColor(Color.BLACK);
		text_e.setTextColor(Color.BLACK);
	}

	@Override
	protected void onDestroy() 
	{
		sManager.unregisterListener(mListener);
		super.onDestroy();
	}
}
